package com.lhm.controller;

import com.lhm.entity.Order;
import com.lhm.service.OrderService;
import com.lhm.tool.RespJson;
import com.lhm.z.ItemBook;
import com.lhm.z.OrderLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/adm/ord")
public class OrderadmController {
    @Autowired
    OrderService orderService;

    @RequestMapping("/addShip")
    ///api/adm/ord/addShip
    public Map<String, Object> addShip(@RequestBody Order ord) {
        new HashMap();
        this.orderService.updateOrdShip(ord);
        return RespJson.pack(200, "添加快递成功 ......", null);
    }


    @RequestMapping({"/findItemBookByOrdid/{ordid}"})

    public Map<String, Object> findItemBookByOrdid(@PathVariable("ordid") int ordid) {
        List<ItemBook> lst = orderService.findItemBookByOrdid(ordid);
        return RespJson.pack(200, "", lst);
    }

    @PostMapping({"/findOrdInfo/{start}/{num}"})
    public Map<String, Object> findOrdInfo(Integer ispay, Date tstart, Date tend, String account, String phone, String nickname, @PathVariable("start") int start, @PathVariable("num") int num) {
        Map<String, Object> map1 = new HashMap();
        Map<String, Object> map = new HashMap();
        map.put("startpage", start * num);
        map.put("pagenum", num);
        if (account != null) {
            map.put("account", account);
        }

        if (phone != null) {
            map.put("phone", phone);
        }

        if (nickname != null) {
            map.put("nickname", nickname);
        }

        if (ispay != null) {
            map.put("ispay", ispay);
        }

        if (tstart != null) {
            map.put("tstart", tstart);
        }

        if (tend != null) {
            map.put("tend", tend);
        }

        System.out.println("/findOrdInfo/{start}/{num} 参数:" + map.toString());
        try {
            List<OrderLine> lst =orderService.findOrdLines(map);
            map1.put("total",orderService.getCount(map));
            map1.put("orders", lst);
        } catch (Exception e) {
            e.printStackTrace();
            return RespJson.pack(500, "查询数据有误....", null);
        }

        return RespJson.pack(200, "", map1);
    }

    //根据订单号获取该订单明细及书本信息
    @RequestMapping({"/findItemInfoByOrdid/{ordid}"})
    public Map<String, Object> findOrdItemInfo(@PathVariable("ordid") int ordid) {
        List<Map<String, Object>> lst = orderService.findItemInfoByOrdid(ordid);
        return RespJson.pack(200, "", lst);
    }
    /**
     * 自动转换日期类型的字段格式
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}
